<!DOCTYPE html>
<html>
<head>
    <title>部门管理</title>
    {% include 'admin/common/header.html' %}

</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">上级分组</label>
                <div class="layui-input-block">
                    <ul id="selectParent" name="parent_id" class="dtree" data-id="0"></ul>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分组名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">下属摄像机</label>
                <div class="layui-input-block">
                    <select id="cameras" name="cameras" xm-select="select5" xm-select-skin="default" lay-search="">
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="group-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
    layui.use(['form', 'jquery', 'dtree', 'select'], function () {
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree
        let select = layui.select
        $.ajax({
            url: '/admin/equipment_cameras/data?page=1&limit=10000',
            dataType: 'json',
            contentType: 'application/json',
            type: 'GET',
            success: function (result) {
                for (var i = 0; i < result.data.length; i++) {
                    $('#cameras').append(`<option value="${result.data[i].id}">${result.data[i].name}</option>`);
                }
                select.render()
            }
        })
        dtree.renderSelect({
            elem: '#selectParent',
            url: '/admin/camera_groups/tree',
            method: 'get',
            selectInputName: {nodeId: 'parent_id', context: 'id'},
            skin: 'layui',
            dataFormat: 'list',
            response: {treeId: 'id', parentId: 'parent_id', title: 'name'},
            selectInitVal: '1'
        })


        form.on('submit(group-save)', function (data) {
            $.ajax({
                url: '/admin/camera_groups/save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))
                            parent.render()
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>
</body>
</html>